<?php
use yii\helpers\Url;

$web = Url::base();
$this->title = 'API异常日志';
?>

<div class="panel panel-default">
    <div class="panel-body">
        <div class="form-inline">
            <div class="col-lg-2">
                <input type="text" class="form-control" id="actionId" placeholder="actionId"
                       value="<?= $actionId ?>">
            </div>
            <div class="col-lg-2">
                <input type="text" class="form-control" id="account" placeholder="account"
                       value="<?= $account ?>">
            </div>
            <div class="col-lg-2">
                <div data-date="" class="input-group date form_datetime-hm">
                    <input type="text" class="form-control" readonly="" size="16" id="startTime"
                           name="startTime" placeholder="开始时间"
                           value="<?= $startTime ?>">
                    <div class="input-group-btn">
                        <button type="button" class="btn btn-default date-reset"><i class="fa fa-times"></i>
                        </button>
                        <button type="button" class="btn btn-default date-set"><i class="fa fa-calendar"></i>
                        </button>
                    </div>
                </div>
            </div>

            <div class="col-lg-2">
                <div data-date="" class="input-group date form_datetime-hm">
                    <input type="text" class="form-control" readonly="" size="16" id="endTime"
                           name="endTime" placeholder="结束时间"
                           value="<?= $endTime ?>">
                    <div class="input-group-btn">
                        <button type="button" class="btn btn-default date-reset"><i class="fa fa-times"></i>
                        </button>
                        <button type="button" class="btn btn-default date-set"><i class="fa fa-calendar"></i>
                        </button>
                    </div>
                </div>
            </div>
            <div class="col-lg-1">
                <button id="btnSearch" type="button" class="btn btn-primary" onclick="filter()">
                    <i class="fa fa-search"></i> 搜索
                </button>
            </div>
        </div>
    </div>
</div>

<div class="panel">
    <div class="panel-body">
        <div class="table-responsive">
            <table class="table table-bordered table-hover table-condensed">
                <thead class="nowrap">
                <tr>
                    <th>action id</th>
                    <th>account</th>
                    <th>time</th>
                    <th>exception</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <?php
                foreach ($records as $record) {
                    ?>
                    <tr>
                        <td><?= $record['actionId'] ?></td>
                        <td><?= $record['account'] ?></td>
                        <td><?= date('Y-m-d H:i:s', $record['createDatetime']) ?></td>
                        <td><?= $record['exceptionMessage'] ?></td>
                        <td>
                            <div class="btn-group">
                                <a class="btn btn-link" data-toggle="modal" data-log-id="<?= $record['_id'] ?>"
                                   href="#detailModal">查看详情</a>
                            </div>
                        </td>
                    </tr>
                    <?php
                }
                ?>
                </tbody>
            </table>
        </div>
        <div class="pull-left">
            <?= \app\widgets\LinkPagerExt::widget(['pagination' => $pagination]) ?>
        </div>
    </div>
</div>


<div aria-hidden="true" role="dialog" tabindex="-1" id="detailModal" class="modal fade">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button>
                <h4 class="modal-title">详情</h4>
            </div>
            <div id="detail_content">
                正在加载数据....
            </div>
        </div>
    </div>
</div>

<link rel="stylesheet" type="text/css" href="<?= $web ?>/css/datetimepicker-custom.css"/>
<script src="<?= $web ?>/js/bootstrap-datetimepicker.js"></script>
<script src="<?= $web ?>/js/pickers-init.js"></script>
<script type="application/javascript">
    $(function () {
        $('#detailModal').on('show.bs.modal', function (e) {
            $("#detail_content").html('<div class="modal-body">正在加载数据....</div>');
            var logId = e.relatedTarget.attributes['data-log-id'].value;
            requestHtml('api-exception-log-partial', {log_id: logId}, true, function (responseHtml) {
                $("#detail_content").html(responseHtml);
            });
        });
    });

    function filter() {
        var actionId = $("#actionId").val();
        var startTime = $("#startTime").val();
        var endTime = $("#endTime").val();

        var href = window.location.href;
        var index = href.indexOf("?page=");
        if (index > 0) {
            href = href.substr(0, index);
        }
        var href_str = href + "?page=1";

        if (actionId != '' && actionId != null) {
            href_str = href_str + "&actionId=" + actionId;
        }

        if (startTime != '' && startTime != null) {
            href_str = href_str + "&startTime=" + startTime;
        }
        if (endTime != '' && endTime != null) {
            href_str = href_str + "&endTime=" + endTime;
        }

        window.location.href = href_str;
    }
</script>
